package com.faya.green.utils;

import com.faya.green.abnormal.ServiceException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * 用户 ：LX
 * 创建时间： 2018/7/8. 16:46
 * 地点：广州
 * 目的: des 加密解密工具类
 * 结果：
 */
public class DesUtil {


    private static Key key;

    /**
     * 设置密钥key
     */
    private static String KEY_STR = "myKey";

    /**
     * 编码
     */
    private static String CHARSETEAME = "UTF-8";

    /**
     * 使用的加密算法
     */
    private static String ALGORITHM = "DES";

    static {
        try {
            //生成DES算法对象
            KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
            //运用SHA1安全策略
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            //设置密钥种子
            secureRandom.setSeed(KEY_STR.getBytes());
            //初始化基于SHA1的算法对象
            generator.init(secureRandom);
            //生成密钥对象
            key = generator.generateKey();
            generator = null;

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new ServiceException("初始化加密失败");
        }
    }


    /**
     * 获取加密后的信息
     * @param str 待加密的字符串
     * @return 加密后的信息
     */
    public static String getEncryptString(String str){
        //基于BASE64编码，接收byte[]并转换为String
        BASE64Encoder base64Encoder = new BASE64Encoder();
        try {
            //按照utf-8的编码
            byte[] bytes = str.getBytes(CHARSETEAME);
            //获取加密对象
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            //初始化密码信息
            cipher.init(Cipher.ENCRYPT_MODE, key);
            //加密
            byte[] doFinal = cipher.doFinal(bytes);
            //将字节转换为字符串
            return base64Encoder.encode(doFinal);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new ServiceException("设置编码失败");
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
            throw new ServiceException("获取加密对象失败");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new ServiceException("获取加密对象失败2");
        } catch (InvalidKeyException e) {
            e.printStackTrace();
            throw new ServiceException("初始化密码信息失败");
        } catch (BadPaddingException e) {
            e.printStackTrace();
            throw new ServiceException("加密失败");
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
            throw new ServiceException("加密失败2");
        }
    }


    /**
     * 解密后的信息
     * @param str 待解密的字符串
     * @return 解密后的字符串
     */
    public static String geDecryptString(String str){
        //基于BASE64编码，接收byte[]并转换成string
        BASE64Decoder base64Decoder = new BASE64Decoder();
        try {
            //将字符串转换为byte
            byte[] bytes = base64Decoder.decodeBuffer(str);
            //获取解密对象
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            //初始化解密信息
            cipher.init(Cipher.DECRYPT_MODE, key);
            //解密
            byte[] doFinal = cipher.doFinal(bytes);
            //返回解密后的信息
            return new String(doFinal, CHARSETEAME);

        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("解密失败");
        }
    }



}
